package com.netsdk.utils;

import cn.hutool.core.util.ObjectUtil;

import com.netsdk.config.AqieConfig;
import com.netsdk.dto.DeviceDto;
import lombok.extern.slf4j.Slf4j;


import com.netsdk.demo.module.LoginModule;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 设备工具类
 * (暂时使用静态数据，实际使用时需要使用数据库中的数据)
 *
 * @author Yan Xu
 * @version 1.0
 * @date 2021/8/6
 * Copyright © goodits
 */
@Slf4j
@Component
public class DeviceUtils {

    private static final Map<String, DeviceDto> DEVICE_DTO_MAP = new LinkedHashMap<>(16);

    @PostConstruct
    void init() {
        AqieConfig bean = SpringUtils.getBean(AqieConfig.class);
        System.out.println("aqieBean " + bean);
        Integer port = bean.getPort();
        String user = bean.getUser();
        String password = bean.getPassword();
        bean.getDevices().forEach(device -> {
            DeviceDto deviceDto = new DeviceDto(device.getId(), device.getIp(), port, user, password);
            DEVICE_DTO_MAP.put(deviceDto.getId(), deviceDto);
        });
        log.info("设备map" + DEVICE_DTO_MAP.toString());
    }


    public static boolean login(String deviceNo) {

        DeviceDto deviceDto = DEVICE_DTO_MAP.get(deviceNo);
        if (ObjectUtil.isEmpty(deviceDto)) {
            log.warn("不存在设备：{}", deviceDto);
            return false;
        }
        return LoginModule.login(deviceDto.getIp(), deviceDto.getPort(), deviceDto.getUser(), deviceDto.getPassword(), deviceNo);
    }

    public static void logout(String deviceNo) {
        LoginModule.logout(deviceNo);
    }
}
